//
//  GradientView.swift
//  Demo
//
//  Created by S JZ on 2024/8/2.
//

import Foundation
import UIKit

public protocol GradientType where Self: UIView { }

public extension GradientType {
    func getGradientLayer() -> CAGradientLayer? { return layer as? CAGradientLayer }
    
    var colors: [Any]? {
        get { getGradientLayer()?.colors }
        set { getGradientLayer()?.colors = newValue }
    }
    
    var locations: [NSNumber]? {
        get { return getGradientLayer()?.locations }
        set { getGradientLayer()?.locations = newValue }
    }
    
    var startPoint: CGPoint {
        get { return getGradientLayer()?.startPoint ?? .zero }
        set { getGradientLayer()?.startPoint = newValue }
    }
    
    var endPoint: CGPoint {
        get { return getGradientLayer()?.endPoint ?? .zero }
        set { getGradientLayer()?.endPoint = newValue }
    }
    
    var type: CAGradientLayerType {
        get { return getGradientLayer()?.type ?? .axial }
        set { getGradientLayer()?.type = newValue }
    }
}

class GradientView: UIView, GradientType {
    // 返回layerClass
    public override class var layerClass: AnyClass {
        return CAGradientLayer.classForCoder()
    }
}

class GradientButton: UIButton, GradientType {
    // 返回layerClass
    public override class var layerClass: AnyClass {
        return CAGradientLayer.classForCoder()
    }
}

class GradientLabel: UILabel, GradientType {
    // 返回layerClass
    public override class var layerClass: AnyClass {
        return CAGradientLayer.classForCoder()
    }
}

